package com.qbw.app.module.vehicle.holder;

import android.content.Context;
import android.view.ViewGroup;

import com.qbw.app.R;
import com.qbw.app.databinding.VehicleHolderElectricAreaModeSelectItemBinding;
import com.qbw.app.model.ElectricArea;
import com.qbw.app.module.common.holder.CommonHolder;

/**
 * @author QBW
 * @date 2019/10/8
 */
public class VehicleElectricAreaModeItemHolder extends CommonHolder<ElectricArea.Mode, VehicleHolderElectricAreaModeSelectItemBinding> {

    public VehicleElectricAreaModeItemHolder(Context context, ViewGroup viewParent) {
        super(context, R.layout.vehicle_holder_electric_area_mode_select_item, viewParent);
    }

    @Override
    public void bindData(int position, ElectricArea.Mode data) {
        super.bindData(position, data);
        mBinding.setMode(data);
        mBinding.executePendingBindings();
        itemView.setOnClickListener(mClickListenerItem);
    }

    @Override
    protected void onClickItem() {
        super.onClickItem();
        int count = getElectricWarningModeSelectedCount();
        if (!mData.isSelected() || count > 1) {
            mData.setSelected(!mData.isSelected());
        }
    }

    private int getElectricWarningModeSelectedCount() {
        if (mContext instanceof Listener) {
            return ((Listener) mContext).onGetElectricWarningModeSelectedCount();
        }
        return 0;
    }

    public interface Listener {
        int onGetElectricWarningModeSelectedCount();
    }
}
